<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <!-- 
      <el-form-item label="归属时间" prop="ownerTime">
        <el-date-picker clearable
          v-model="queryParams.ownerTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择归属时间">
        </el-date-picker>
      </el-form-item>
       -->
       <el-form-item label="选择日期">
        <el-row :gutter="10" class="mb8">
          <!-- <el-col :span="1.5">
            <el-date-picker
              v-model="dateRange"
              type="daterange"
              align="left"
              unlink-panels
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              value-format="yyyy-MM-dd"
              @change="handleDateChange"
              :picker-options="pickerOptions">
            </el-date-picker>
          </el-col> -->
          <el-col :span="1.5">
            <el-form-item style="">
              <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
              <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['dr:base:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="sampleSellAnalysisList" 
    @selection-change="handleSelectionChange"
    :row-class-name="tableRowClassName">
      <el-table-column label="开始寄样日期" align="center" prop="startTime" />
      <el-table-column label="达人" align="center" prop="name" />
      <el-table-column label="达人号" align="center" prop="awemeId" />
      <el-table-column label="寄样数量" align="center" prop="sampleNum" />
      <el-table-column label="交付数量" align="center" prop="deliveryQuantity" />
      <el-table-column label="交付率" align="center" prop="deliveryRate" />
      <el-table-column label="寄样天数" align="center" prop="dayNum" />
    </el-table>
  </div>
</template>

<script>
import { sampleSellAnalysis } from "@/api/dr/sampleSellAnalysis"

export default {
  name: "sampleSellAnalysis",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 达人列页信息表格数据
      sampleSellAnalysisList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        dayDate: null,
        dayDateStart: null,
        dayDateEnd: null,
        awemeId: null,
        awemeName: null,
        productName: null,
        productId: null
        
      },
      dateRange: [],
      dayDateStart: null,
      dayDateEnd: null,

      // 表单参数
      form: {}
    }
  },
  created() {
    this.getList();
    this.initDateRange();
  },
  methods: {
    /** 查询销售数据信息列表 */
    getList() {
      this.loading = true
      sampleSellAnalysis(this.queryParams).then(response => {
        this.sampleSellAnalysisList = response.data
        console.log(JSON.stringify(this.sampleSellAnalysisList))
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        awemeId: null,
        unionUid: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.dayDateStart = this.dayDateStart;
      this.queryParams.dayDateEnd = this.dayDateEnd;
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.awemeId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    // 日期变化时更新dayDateStart和dayDateEnd字段
    handleDateChange(val) {
      if (val && val.length === 2) {
        this.dayDateStart = val[0]; // 开始时间赋值给dayDateStart
        this.dayDateEnd = val[1]; // 结束时间赋值给dayDateEnd
      } else {
        this.dayDateStart = null;
        this.dayDateEnd = null;
      }
    },
    // 给表格加上各种颜色
    tableRowClassName({row, rowIndex}) {
        if (row.sampleNum > 40 && row.deliveryRate > 0.5) {
          return 'success-row';
        } else if (row.sampleNum > 30 && row.deliveryRate > 0.2) {
          return 'blue-row';
        } else if (row.sampleNum > 20 && row.deliveryRate > 0.1) {
          return 'warning-row';
        } else if (row.sampleNum > 10 && row.deliveryRate === '0' ) {
          return 'danger-row';
        }
        return '';
    },
    // 如果已有 dayDateStart 和 dayDateEnd 值，初始化时设置dateRange
    initDateRange() {
      if (this.dayDateStart && this.dayDateEnd) {
        this.dateRange = [this.dayDateStart, this.dayDateEnd];
      }
    },
  
    /** 导出按钮操作 */
    handleExport() {
      this.download('dr/base/export', {
        ...this.queryParams
      }, `base_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>

<style>
  .el-table .success-row{
    background: #bff7a3;
  }
  .el-table .blue-row{
    background: #79b9f9;
  }
  .el-table .warning-row{
    background: #fdcc83;
  }
  .el-table .danger-row{
    background: #F56C6C;
  }
</style>
